package com.qidian.hw.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qidian.hw.pojo.CaseInfo;
import com.qidian.hw.service.CaseInfoService;
import com.qidian.hw.service.impl.CaseInfoServiceImpl;
import com.qidian.hw.tools.Const;

import com.qidian.hw.tools.JSONTools;
import com.qidian.hw.tools.dto.Pager;

/**
* @author 郭浩
*
*@company 起点编程
*
* 2022年8月19日 下午7:46:57
*/
public class CaseInfoServlet extends HttpServlet{
	CaseInfoService caseInfoService = new CaseInfoServiceImpl();
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String op = req.getParameter("op");
		switch (op) {
			//保存
		case "save":
			save(req,resp);
			break;
			//回显
		case "input":
			input(req,resp);
			break;
			//表单传回值修改
		case "update":
			update(req,resp);
			break;
			//列表
		case "list":
			list(req,resp);
			break;
			//删除
		case "delete":
			delete(req,resp);
			break;
		case "deletes":
			deletes(req,resp);
			break;
		case "query":
			query(req,resp);
			break;
		default:
			super.service(req, resp);
		}
	}
	
	public void deletes(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String result = "0";
		String [] ids = req.getParameterValues("caseId");
		if(ids!=null && ids.length > 0) {
			result = caseInfoService.deletes(ids) + "";
		}
		resp.setContentType(Const.RES_PLAIN);
		resp.getWriter().write(result);
	}

	public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String caseTitle = req.getParameter("caseTitle");
		String casetDesc = req.getParameter("casetDesc");
		String caseImg = req.getParameter("caseImg");
		String caseDetails = req.getParameter("caseDetails");
		String caseDetailsImg = req.getParameter("caseDetailsImg");
		CaseInfo caseInfo = new CaseInfo(caseTitle, casetDesc, caseImg, caseDetails, caseDetailsImg);
		int result = caseInfoService.caseInfoSave(caseInfo);
		resp.setContentType(Const.RES_PLAIN);
		resp.getWriter().write(result + "");
	}
	
	public void input(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String caseIdStr = req.getParameter("caseId");
		long caseId = 0;
		try {
			caseId = Long.parseLong(caseIdStr);
		} catch (Exception e) {
		}
		CaseInfo caseInfo = caseInfoService.queryCaseInfoById(caseId);
		String caseInfoStr = JSONTools.object2json(caseInfo);
		resp.setContentType(Const.RES_JSON);
		resp.getWriter().write(caseInfoStr);
	}
	
	public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String caseIdStr = req.getParameter("caseId");
		long caseId = 0;
		try {
			caseId = Long.parseLong(caseIdStr);
		}catch(Exception e) {}
		String caseTitle = req.getParameter("caseTitle");
		String caseDesc = req.getParameter("caseDesc");
		String caseImg = req.getParameter("caseImg");
		String caseDetails = req.getParameter("caseDetails");
		String caseDetailsImg = req.getParameter("caseDetailsImg");
		CaseInfo caseInfo = new CaseInfo(caseTitle, caseDesc, caseImg, caseDetails, caseDetailsImg);
		caseInfo.setCaseId(caseId);
		int result = caseInfoService.caseInfoUpdate(caseInfo);
		resp.setContentType(Const.RES_PLAIN);
		resp.getWriter().write(result + "");
	}
	
	public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ArrayList<CaseInfo> al = new ArrayList<CaseInfo>();
		al = caseInfoService.caseInfoList();
		String caseInfosStr = JSONTools.object2json(al);
		resp.setContentType(Const.RES_JSON);
		resp.getWriter().write(caseInfosStr);
	}
	
	public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String caseInfoIdStr = req.getParameter("caseId");
		long caseInfoId = 0;
		try {
			caseInfoId = Long.parseLong(caseInfoIdStr);
		} catch (Exception e) {
		}
		int result = caseInfoService.caseInfoDelete(caseInfoId);
		resp.setContentType(Const.RES_PLAIN);
		resp.getWriter().write(result + "");
	}
	
	public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String title = req.getParameter("title");
		String pageStr = req.getParameter("page");
		String sizeStr = req.getParameter("size");
		int page = 1;
		int size = 10;
		try {
			page = Integer.parseInt(pageStr);
			size = Integer.parseInt(sizeStr);
		} catch (Exception e) {
		}
		Pager pager = caseInfoService.queryByPage(page, size, title);
		String pagerStr = JSONTools.object2json(pager);
		resp.setContentType(Const.RES_JSON);
		resp.getWriter().write(pagerStr);
	}
}
